<?php
namespace PsiFramework\Applications\AlloMediaUser\Classes\Actions;

import("Applications.AlloMediaPortal.Classes.Actions.AlloMediaUserAction");
import("Classes.Util.HTTP.AuthenticationManager");
import("Classes.Factory.DAOFactory");
import("Plugins.Shop.Classes.Model.Order");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\InformationPart;
use \PsiFramework\Classes\Util\HTTP\AuthenticationManager;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Classes\Util\Object\EMail;
use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Plugins\Shop\Classes\Model\Order;
use \PsiFramework\Classes\Util\Manager\ParametersManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de l'index
 *
 * @package Actions.Pages
 */
class PaymentValidationAction extends AbstractAction
{

    /**
     * Constructor
     * 
     * @param ActionParameters $actionParameters The parameters
     * 
     * @return void
     */
    public function __construct(ActionParameters $actionParameters)
    {
        parent::__construct($actionParameters);
    }

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     *@return void
     */
    public function execute()
    {
        parent::execute();
        
        $password = ShopAction::getPaymentPassword();
        $params = ShopAction::getPaymentParamsList();
        
        $shaString = '';
        foreach ($params as $paramName) {
            $paramValue = $this->_request->getParameter($paramName);
            $shaString .= strtoupper($paramName).'='.$paramValue.$password;
        }
        $shaSign = $this->_request->getParameter('SHASIGN');
        $calculatedSha = strtoupper(sha1($shaString));
        $status = $this->_request->getParameter('STATUS');
        
        if ($shaSign == $calculatedSha) {
            $orderNumber = $this->_request->getParameter('orderID');
            $orderDao = DAOFactory::getDAO('Order');
            $order = $orderDao->retrieveByNumber($orderNumber, false, false);
            $order->setStatus($status);
            $order->setMeanOfPayment('CREDIT_CARD');
            $recalculate = !$order->isPaid();
            $validate = ($status==Order::$paymentRequested['code'] && $recalculate);
            
            ShopAction::getOrder($order, $recalculate, true, $validate, false);
            
            if ($validate && $order->isPaid()) {
                $pageDao = DAOFactory::getDAO('Page');
                $page = $pageDao->retrieveByPartialName('PaymentConfirmation', 'fr', true);
                
                $sender = ParametersManager::getInstance()->getApplicationsParameters()->getContact();
                $user = $order->getUser();
                $customerEmail = $user->getEmail();
                
                $mailContent = $page->getContent();
                $mailContent = str_replace('#login#', $user->getLogin(), $mailContent);
                $mailContent = str_replace('#name#', $user->getSurName(), $mailContent);
                $mailContent = str_replace('#firstName#', $user->getFirstName(), $mailContent);
                
                $email = new EMail($sender, $customerEmail, $page->getTitle(), nl2br($mailContent));
                //send email to the user
                $email->send();
            }
        }
        exit;
    }
}
